package com.teradyne.merlin2.ui.client.views;

import static com.google.gwt.dom.client.Style.Unit.PCT;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class AppLayoutImpl implements AppLayout {

	interface AppLayoutUiBinder extends UiBinder<LayoutPanel, AppLayoutImpl> {
	}

	private static AppLayoutUiBinder uiBinder = GWT
			.create(AppLayoutUiBinder.class);

	private static final int TOP_HEIGHT = 10;
	private static final int BOTTOM_HEIGHT = 10;
	private static final int LEFT_WIDTH = 20;
	private static final int RIGHT_WIDTH = 20;

	private final LayoutPanel mainLayoutPanel;

	@UiField
	SimplePanel topPanel;

	@UiField
	SimplePanel rightSidePanel;

	@UiField
	SimplePanel leftSidePanel;

	@UiField
	SimplePanel bottomPanel;

	@UiField
	SimplePanel centerPanel;

	@Inject
	public AppLayoutImpl() {
		mainLayoutPanel = uiBinder.createAndBindUi(this);
	}

	@Override
	public LayoutPanel getMainLayoutPanel() {
		return mainLayoutPanel;
	}

	@Override
	public AcceptsOneWidget getTopContainer() {
		return new AcceptsOneWidget() {

			@Override
			public void setWidget(IsWidget w) {
				Widget widget = Widget.asWidgetOrNull(w);
				topPanel.setWidget(widget);
			}
		};
	}

	@Override
	public AcceptsOneWidget getBottomContainer() {
		return new AcceptsOneWidget() {

			@Override
			public void setWidget(IsWidget w) {
				Widget widget = Widget.asWidgetOrNull(w);
				bottomPanel.setWidget(widget);
			}
		};
	}

	@Override
	public AcceptsOneWidget getLeftSideContainer() {
		return new AcceptsOneWidget() {

			@Override
			public void setWidget(IsWidget w) {
				Widget widget = Widget.asWidgetOrNull(w);
				leftSidePanel.setWidget(widget);
			}
		};
	}

	@Override
	public AcceptsOneWidget getRightSideContainer() {
		return new AcceptsOneWidget() {

			@Override
			public void setWidget(IsWidget w) {
				Widget widget = Widget.asWidgetOrNull(w);
				rightSidePanel.setWidget(widget);
			}
		};
	}

	@Override
	public AcceptsOneWidget getCenterContainer() {
		return new AcceptsOneWidget() {

			@Override
			public void setWidget(IsWidget w) {
				Widget widget = Widget.asWidgetOrNull(w);
				centerPanel.setWidget(widget);
			}
		};
	}

	@Override
	public void setDefaultLayout() {
		mainLayoutPanel.setWidgetTopHeight(topPanel, 0, PCT, TOP_HEIGHT, PCT);

		mainLayoutPanel.setWidgetBottomHeight(bottomPanel, 0, PCT,
				BOTTOM_HEIGHT, PCT);

		mainLayoutPanel.setWidgetLeftWidth(leftSidePanel, 0, PCT, LEFT_WIDTH,
				PCT);
		mainLayoutPanel.setWidgetTopBottom(leftSidePanel, TOP_HEIGHT, PCT,
				BOTTOM_HEIGHT, PCT);

		mainLayoutPanel.setWidgetRightWidth(rightSidePanel, 0, PCT,
				0, PCT);
		mainLayoutPanel.setWidgetTopBottom(rightSidePanel, 0, PCT,
				0, PCT);

		mainLayoutPanel.setWidgetTopBottom(centerPanel, TOP_HEIGHT, PCT,
				BOTTOM_HEIGHT, PCT);
//		mainLayoutPanel.setWidgetLeftWidth(centerPanel, LEFT_WIDTH, PCT, 0, PCT);
		mainLayoutPanel.setWidgetLeftRight(centerPanel, LEFT_WIDTH, PCT,
				0, PCT);

	}

}
